package com.sky.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.DefaultAddressConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.service.AddressBookService;
import com.sky.mapper.AddressBookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 马毅爽
* @description 针对表【address_book(地址簿)】的数据库操作Service实现
* @createDate 2023-11-09 15:52:45
*/
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook>
    implements AddressBookService{

    @Autowired
    private AddressBookMapper addressBookMapper;
    @Override
    public List<AddressBook> listByUserId() {
        return lambdaQuery().eq(AddressBook::getUserId,BaseContext.getCurrentId()).list();
    }

    @Override
    public void add(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        save(addressBook);
    }

    @Override
    public void setDefault(AddressBook addressBook) {
        lambdaUpdate().set(AddressBook::getIsDefault,DefaultAddressConstant.IS_NOT_DEFAULT)
                .eq(AddressBook::getUserId,BaseContext.getCurrentId())
                .update();

        lambdaUpdate().set(AddressBook::getIsDefault, DefaultAddressConstant.IS_DEFAULT)
                .eq(AddressBook::getId,addressBook.getId())
                .update();
    }

    @Override
    public AddressBook queryDefault() {
        return lambdaQuery()
                .eq(AddressBook::getIsDefault,DefaultAddressConstant.IS_DEFAULT)
                .eq(AddressBook::getUserId,BaseContext.getCurrentId())
                .list().get(0);
    }
}




